<template>
  <div style="height: 6vh;background-color:#fff;padding: 0 20px 10px;">
    <span style="line-height:60px;font-size:20px;">围栏管理</span>
    <el-button type="primary" style="float:right;margin-top:12px;" @click="router.push('/geofenceMap')">新建围栏</el-button>
  </div>
  <el-card>
      <el-form :inline="true"> 
          <el-form-item label="围栏名称" style="width:270px;">
            <el-input placeholder="请输入" v-model="searchGeofenceForm.name"></el-input>
          </el-form-item>
          <el-form-item label="围栏状态" style="width:270px;">
            <el-select placeholder="请选择" v-model="searchGeofenceForm.status">
              <el-option label="启用" value="1"/>
              <el-option label="禁用" value="0"/>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="loadGeofence">查询</el-button>
            <el-button @click="resetSearch">重置</el-button>
          </el-form-item>
      </el-form>
  </el-card>
  <el-card style="margin:10px 0;">
    <el-table stripe :data="geoTableData">
      <el-table-column label="编号" prop="id" type="index" align="center" width="100"></el-table-column>
      <el-table-column label="围栏名称" prop="name" align="center"></el-table-column>
      <el-table-column label="围栏坐标点" prop="position" align="center" :show-overflow-tooltip="true" width="500"></el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center"></el-table-column>
      <el-table-column label="围栏状态" align="center">
        <template #default="scope">
          <el-switch inactive-value="0" active-value="1" v-model="scope.row.status" @change="changeStatus(scope.row.id,scope.row.status)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="loadVehicle(scope.row.id)">管理车辆</el-button>
          <el-button type="primary" link @click="dropGeofence(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <el-dialog title="已绑定车辆" v-model="bindVehicleDialogVisible" style="padding:40px;">
    <el-button type="primary" style="margin:10px 0;" @click="loadUnBindVehicle">添加绑定车辆</el-button>
    <el-table :data="bindVehicleArr">
      <el-table-column label="车辆品牌" prop="brand" align="center"></el-table-column>
      <el-table-column label="车牌号" prop="license" align="center"></el-table-column>
      <el-table-column label="车辆类型" prop="type" align="center"></el-table-column>
      <el-table-column label="操作" fixed="right" width="200" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="removeBindVehicle(scope.row.id)">移除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
  <el-dialog title="可绑定车辆列表" v-model="AddBindVehicleDialogVisible" :before-close="handleClose">
    <el-table :data="unBindVehicleArr">
      <el-table-column label="编号" type="index" align="center" width="80px"></el-table-column>
      <el-table-column label="车辆品牌" prop="brand" align="center"></el-table-column>
      <el-table-column label="车牌号" prop="license" align="center"></el-table-column>
      <el-table-column label="车辆类型" prop="type" align="center"></el-table-column>
      <el-table-column label="车辆状态" prop="status" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="bindVehicle(scope.row.id)">绑定</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>

<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage, ElMessageBox} from "element-plus";
import router from "@/router";
  const bindVehicleDialogVisible = ref(false);
  const AddBindVehicleDialogVisible = ref(false);
  const searchGeofenceForm = ref({
    name: '',
    status: ''
  });
  const geoTableData = ref([]);
  const bindVehicleArr = ref([]);
  const unBindVehicleArr = ref([]);
  var geoId;
  const loadGeofence = () => {
    let data = qs.stringify(searchGeofenceForm.value);
    axios.get(BASE_URL + '/v1/geofence/queryGeofence?' + data).then(response => {
      if (response.data.code == 2000) {
        geoTableData.value = response.data.data;
      }else {
        ElMessage.error(response.data.msg);
      }
    })
  };
  onMounted(() => {
    loadGeofence();
  });
  const resetSearch = () => {
    searchGeofenceForm.value = {
      name: '',
      status: ''
    };
    loadGeofence();
  };
  const loadVehicle = (geofenceId) => {
    geoId = geofenceId;
    bindVehicleDialogVisible.value = true;
    axios.get(BASE_URL + '/v1/vehicle/list?geofenceId=' + geoId).then(response => {
      if (response.data.code == 2000) {
        bindVehicleArr.value = response.data.data;
      }else {
        ElMessage.error(response.data.msg);
      }
    })
  };
  const loadUnBindVehicle = ()  => {
    AddBindVehicleDialogVisible.value = true;
    axios.get(BASE_URL + '/v1/vehicle/list?geofenceBindStatus=0').then(response => {
      if (response.data.code == 2000) {
        unBindVehicleArr.value = response.data.data;
      }else {
        ElMessage.error(response.data.msg);
      }
    })
  };
  const bindVehicle = (vehicleId) => {
    axios.post(BASE_URL + '/v1/vehicle/bind/' + vehicleId + '/' + geoId).then(response => {
      if (response.data.code == 2000) {
        ElMessage.success("绑定成功！");
        loadUnBindVehicle();
      } else {
        ElMessage.error(response.data.msg);
      }
    })
  };
  const handleClose = () => {
    loadVehicle(geoId);
    AddBindVehicleDialogVisible.value = false;
  };
  const removeBindVehicle = (vehicleId) => {
    axios.post(BASE_URL + '/v1/vehicle/unbind/' + vehicleId).then(response => {
      if (response.data.code == 2000) {
        ElMessage.success("移除成功！");
        loadVehicle(geoId);
      } else {
        ElMessage.error(response.data.msg);
      }
    })
  };
  const dropGeofence = (geofenceId) => {
    ElMessageBox.confirm('确定删除该围栏吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      axios.post(BASE_URL + '/v1/geofence/delete/' + geofenceId).then(response => {
        if (response.data.code == 2000) {
          ElMessage.success("删除成功！");
          loadGeofence();
        } else {
          ElMessage.error(response.data.msg);
        }
      })
    }).catch(() => {});
  };
  const changeStatus = (geofenceId, status) => {
    let data = qs.stringify({id: geofenceId,status: status});
    axios.post(BASE_URL + '/v1/geofence/save', data).then(response => {
      if (response.data.code == 2000) {
        ElMessage.success("修改成功！");
      } else {
        ElMessage.error(response.data.msg);
      }
    })
  };
</script>

<style scoped>

</style>